package com.ly.airline.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ly.airline.pojo.User;
import com.ly.airline.service.UserService;
import com.ly.airline.utils.HttpResult;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * @author ：ly
 * @ClassName UserController
 * @date ：Created in 2022/4/29 9:11
 * @description： 用户接口
 * @modified By：
 * @version: 1.0
 */
@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     *@param id
	* @param model
     * @return java.lang.String
     * @author ly
     * @description: 后台获取用户详情
     * @date 2022/4/30 0:41
     */
    @GetMapping("getUserDetail/{id}")
    public String getUserDetail(@PathVariable Integer id, Model model){
        User user = userService.getById(id);
        model.addAttribute("user",user);
        return "admin/user/userXinxi";
    }

    /**
     *@param current
	* @param pageSize
	* @param model
     * @return java.lang.String
     * @author ly
     * @description: 后台获取用户列表
     * @date 2022/4/30 0:42
     */
    @GetMapping("getUserList/{current}/{pageSize}")
    public String getUserList(@PathVariable Integer current, @PathVariable Integer pageSize, Model model){
        IPage page = new Page(current,pageSize);
        userService.page(page);
        model.addAttribute("pagers",page);
        return "admin/user/userMana";
    }

    /**
     *@param id
     * @return com.ly.airline.utils.HttpResult
     * @author ly
     * @description: 删除用户
     * @date 2022/4/30 0:42
     */
    @GetMapping("deleteUser/{id}")
    @ResponseBody
    public HttpResult deleteUser(@PathVariable Integer id){
        userService.removeById(id);
        return HttpResult.success(true,"删除成功");
    }

    /**
     *@param user
     * @return com.ly.airline.utils.HttpResult
     * @author ly
     * @description: 注册前台用户
     * @date 2022/4/30 0:42
     */
    @PostMapping("register")
    @ResponseBody
    public HttpResult register(User user){
        //校验注册数据
        if (Strings.isBlank(user.getUserName())||Strings.isBlank(user.getUserPw())||Strings.isBlank(user.getUserSex())){
            return HttpResult.success(false,"请填写完整信息");
        }
        //查询用户名是否已存在
        LambdaQueryWrapper<User> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(User::getUserName,user.getUserName());
        User one = userService.getOne(queryWrapper);
        if (BeanUtil.isNotEmpty(one)){
            return HttpResult.success(false,"用户名已存在");
        }
        //数据库添加用户
        userService.save(user);
        return HttpResult.success(true,"注册成功");
    }
}
